Coverage Report

Created: 2026-11-19 22:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\cssh-rs\cssh-rs\cssh-rs-protocol\src\v1\version.rs
Line
Count
Source
1
//! Protocol version and endpoint role.
2
3
use std::fmt;
4
use std::str::FromStr;
5
6
use serde_derive::{Deserialize, Serialize};
7
8
/// Protocol version as a `major.minor` pair.
9
///
10
/// Serialized on the wire as the textual `"major.minor"` form (e.g. `"1.0"`),
11
/// matching the handshake examples and the plugin manifest. A differing major
12
/// version between peers is fatal; a differing minor is tolerated.
13
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
14
#[serde(try_from = "String", into = "String")]
15
pub struct ProtocolVersion {
16
    /// Major version; a mismatch between peers is a hard error.
17
    pub major: u16,
18
    /// Minor version; a mismatch between peers is tolerated.
19
    pub minor: u16,
20
}
21
22
impl ProtocolVersion {
23
    /// Return a version with the given major and minor components.
24
    ///
25
    /// # Arguments
26
    /// * `major` - Major version component.
27
    /// * `minor` - Minor version component.
28
    ///
29
    /// # Returns
30
    /// The corresponding [`ProtocolVersion`].
31
20
    pub const fn new(major: u16, minor: u16) -> Self {
32
20
        return Self { major, minor };
33
20
    }
34
}
35
36
impl fmt::Display for ProtocolVersion {
37
13
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38
13
        return write!(f, "{}.{}", self.major, self.minor);
39
13
    }
40
}
41
42
/// Error returned when a protocol version string cannot be parsed.
43
#[derive(Clone, Debug, PartialEq, Eq)]
44
pub struct ParseProtocolVersionError(String);
45
46
impl fmt::Display for ParseProtocolVersionError {
47
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48
0
        return write!(f, "invalid protocol version: {}", self.0);
49
0
    }
50
}
51
52
impl std::error::Error for ParseProtocolVersionError {}
53
54
impl FromStr for ProtocolVersion {
55
    type Err = ParseProtocolVersionError;
56
57
13
    fn from_str(s: &str) -> Result<Self, Self::Err> {
58
13
        let (
major11
,
minor11
) = match s.split_once('.') {
59
11
            Some(parts) => parts,
60
2
            None => return Err(ParseProtocolVersionError(s.to_string())),
61
        };
62
11
        let major = match major.parse::<u16>() {
63
11
            Ok(value) => value,
64
0
            Err(_) => return Err(ParseProtocolVersionError(s.to_string())),
65
        };
66
11
        let 
minor10
= match minor.parse::<u16>() {
67
10
            Ok(value) => value,
68
1
            Err(_) => return Err(ParseProtocolVersionError(s.to_string())),
69
        };
70
10
        return Ok(Self { major, minor });
71
13
    }
72
}
73
74
impl TryFrom<String> for ProtocolVersion {
75
    type Error = ParseProtocolVersionError;
76
77
10
    fn try_from(value: String) -> Result<Self, Self::Error> {
78
10
        return value.parse();
79
10
    }
80
}
81
82
impl From<ProtocolVersion> for String {
83
12
    fn from(value: ProtocolVersion) -> Self {
84
12
        return value.to_string();
85
12
    }
86
}
87
88
/// Which endpoint an [`Hello`](crate::v1::handshake::Hello) came from.
89
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
90
#[serde(rename_all = "snake_case")]
91
pub enum Role {
92
    /// The coordinating daemon.
93
    Daemon,
94
    /// A per-host client.
95
    Client,
96
    /// A role not defined in this protocol minor version.
97
    #[serde(other)]
98
    Unknown,
99
}
100
101
#[cfg(test)]
102
#[path = "../tests/v1/test_version.rs"]
103
mod tests;